package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.config.player.UserDecorationInfoResult;
import com.yanqu.road.entity.config.zhaoshang.ZsProjectInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.zhaoshang.ZsModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsConfigMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;

@Cmd(code = Protocol.S_ZS_PROJECT_OPEN, desc = "承包项目")
public class ZsProjectOpenCmd extends AbstractCommand {

    private int templateId;

    private int num;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsProjectOpenReqMsg reqMsg = ZhaoShangProto.ZsProjectOpenReqMsg.parseFrom(packet.getBytes());
        templateId = reqMsg.getTemplateId();
        num = reqMsg.getNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.ZhaoShang.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        if(!ZsMgr.isZsSystemOpenTime()){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        ZsProjectInfo zsProjectInfo = ZsConfigMgr.getZsProjectInfoMap().get(templateId);
        if(zsProjectInfo == null){
            ret = GameErrorCode.E_ZS_PROJECT_CONFIG_NOT_FOUND;
            return;
        }
        if(num <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        //按照vip等级获取项目开启倍数
        int limitMultiple = player.getModule(ZsModule.class).getLimitMultiple();
        //可叠加倍数
        if(num > limitMultiple){
            ret = GameErrorCode.E_ZS_PROJECT_OPEN_EXCEED_MAX_MULTIPLE;
            return;
        }

        //倍数
        Property consume = PropertyHelper.parseStringToProperty(zsProjectInfo.getConsume());
        consume.rideProperty(num);

        //消耗判断
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(consume)) {
            player.notifyListener(eGamePlayerEventType.RechargeZsOpen.getValue(), 0);
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        player.getModule(PreReduceModule.class).preReduce(PropertyHelper.parsePropertyToString(consume));

        //userBaseInfo在前，检测下装饰过期
        UserBaseInfo userBaseInfo = UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId());
        UserDecorationInfoResult result = player.getModule(DecorationModule.class).getCurMaxDecorationInfo();
        int decorationId = result.decorationId;
        long decorationOverTime = result.decorationOverTime;
        //称号加成
        int decorationAddition = ZsMgr.getDecorationAddition(decorationId);

        ZhaoShangProto.CrossZsProjectOpenReqMsg.Builder crossReqMsg = ZhaoShangProto.CrossZsProjectOpenReqMsg.newBuilder();
        crossReqMsg.setNum(num);
        crossReqMsg.setTemplateId(templateId);
        crossReqMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        crossReqMsg.setAddition(decorationAddition);
        crossReqMsg.setTitleAddition(ZsMgr.getTitleAddition(player.getTitleId()));
        crossReqMsg.setOpenNumLimit(player.getModule(ZsModule.class).getZsOpenNum());
        crossReqMsg.setGroupId(ZsMgr.getGroupId(GameServer.getInstance().getServerId()));  //分组id
        crossReqMsg.setDecorationOverTime(decorationOverTime);
        crossReqMsg.setSkillAddition((int)CurioMgr.getSkillAdditionBySkillType(eSkillType.CurioZSTokenSkill.getValue(), player, 0));
        //开启时间限制
        crossReqMsg.addTimeLimit(ConfigMgr.getZsProjectOpenTime().get(0));
        crossReqMsg.addTimeLimit(ConfigMgr.getZsProjectOpenTime().get(1));
        player.sendPacket(CrossProtocol.C2_ZHAO_SHANG_PROJECT_OPEN, crossReqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ZhaoShangProto.ZsProjectOpenRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectOpenRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setTemplateId(templateId);
            player.sendPacket(ClientProtocol.U_ZS_PROJECT_OPEN, respMsg);
        }
    }
}
